<template>
  <div class="AuditList">
    <el-card>
      <div class="searchBox">
        <el-input size="small" placeholder="请输入内容" v-model="id">
          <template slot="prepend">用户ID</template>
        </el-input>
        <el-date-picker
          size="small"
          v-model="timeArr"
          type="daterange"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="timeOptions"
          value-format="yyyy-MM-dd"
          @change="focus"
        >
        </el-date-picker>
        <el-button class="btn" size="small" @click="search">搜 索</el-button>
      </div>
      <el-radio-group
        class="margin"
        size="small"
        v-model="type"
        @change="search"
        fill="#7b94d8"
      >
        <el-radio-button label="1">赠送金币:{{ countGold }}</el-radio-button>
        <el-radio-button label="2">赠送宝石:{{ countDiamond }}</el-radio-button>
        <el-radio-button label="3">赠送VIP:{{ countVip }}</el-radio-button>
        <el-radio-button label="4">扣除:{{ countDeduct }}</el-radio-button>
      </el-radio-group>
      <el-table
        v-loading="loading"
        :data="tableData"
        style="width: 100%; margin-top: 10px"
        :header-cell-style="{ background: '#f5f7fa' }"
        @selection-change="handleSelectionChange"
      >
        <template slot="empty">
          <el-empty :image-size="200"></el-empty>
        </template>
        <el-table-column type="selection" label="全选" width="55">
        </el-table-column>
        <el-table-column prop="create_time" label="提交时间"> </el-table-column>
        <el-table-column prop="id_code" label="用户ID"> </el-table-column>
        <el-table-column label="类型">
          <template slot-scope="scope">
            {{
              scope.row.type == 1
                ? "金币"
                : scope.row.type == 2
                ? "宝石"
                : "VIP"
            }}
          </template>
        </el-table-column>

        <el-table-column prop="number" key="123" label="数量" v-if="type != 3">
        </el-table-column>
        <el-table-column
          prop="vip_time_limit"
          key="1234"
          v-if="type == 3"
          label="时限"
        >
          <template slot-scope="scope">
            {{
              scope.row.vip_time_limit == 1
                ? "月度"
                : scope.row.vip_time_limit == 2
                ? "季度"
                : "年度"
            }}
          </template>
        </el-table-column>
        <el-table-column prop="apply_reason" label="原因"> </el-table-column>
        <el-table-column prop="apply_account" label="申请人"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="small" plain type="info" @click="nopass(scope.row)"
              >驳回</el-button
            >
            <el-button
              size="small"
              plain
              type="success"
              @click="pass([scope.row.id])"
              >同意</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div style="margin: 20px 0 0">
        <el-button size="small" plain type="info" @click="plnopass"
          >全部驳回</el-button
        >
        <el-button size="small" plain type="success" @click="plpass"
          >全部同意</el-button
        >
      </div>
      <pagination
        :total="total"
        :page.sync="page"
        :limit.sync="pageSize"
        @pagination="getList"
      />
    </el-card>
    <el-dialog title="驳回" :visible.sync="dialogVisible" width="30%">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="驳回理由">
          <el-radio-group
            v-model="form.quick"
            @change="form.reason = form.quick"
          >
            <el-radio label="数额超出范围">数额超出范围</el-radio>
            <el-radio label="赠送次数过多">赠送次数过多</el-radio>
            <el-radio label="">手动输入理由</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-show="!form.quick" label="">
          <el-input placeholder="请输入理由" v-model="form.reason"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitNopass">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from "@/components/Pagination"; // 分页
import {
  adminSystemGiveSystemGiveVerify,
  adminSystemGiveSystemVerify,
} from "@/api/AuditManagement";
export default {
  components: { Pagination },
  data() {
    return {
      loading: false,
      type: "1",
      id: "",
      countGold: 0,
      countDiamond: 0,
      countVip: 0,
      countDeduct: 0,
      timeArr: [],
      startTime: "",
      endTime: "",
      timeOptions: {
        disabledDate(platformValue) {
          return platformValue.getTime() > Date.now() - 8.64e6;
        },
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 6);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      tableData: [],
      total: 0,
      page: 1,
      pageSize: 10,
      targetArr: [],
      targetId: [],
      dialogVisible: false,
      form: {
        quick: "1",
        reason: "",
      },
    };
  },
  methods: {
    focus() {
      if (!this.timeArr) {
        this.timeArr = [];
      }
      this.startTime = this.timeArr[0];
      this.endTime = this.timeArr[1];
    },
    handleSelectionChange(val) {
      this.targetId = val.map((item) => {
        return item.id;
      });
    },
    async getList() {
      this.loading = true;
      const res = await adminSystemGiveSystemGiveVerify({
        verify_type: "1",
        type: this.type,
        page: this.page,
        pageSize: this.pageSize,
        startTime: this.startTime,
        endTime: this.endTime,
        user_id: this.id,
      });
      this.loading = false;
      if (res.code == 0) {
        this.tableData = res.data.list;
        this.total = parseInt(res.data.count);
        this.countGold = res.data.countGold;
        this.countDiamond = res.data.countDiamond;
        this.countVip = res.data.countVip;
        this.countDeduct = res.data.countDeduct;
      }
    },
    search() {
      this.page = 1;
      this.getList();
    },
    nopass(obj) {
      this.form = {
        quick: "1",
        reason: "",
      };
      this.targetId = [obj.id];
      this.dialogVisible = true;
    },
    async pass(id) {
      const res = await adminSystemGiveSystemVerify({
        id,
        verify_type: "1",
        state: "1",
        charge_approve_name: localStorage.getItem("SET_NAME"),
        charge_account: localStorage.getItem("SET_ACCOUNT"),
      });
      if (res.code == 0) {
        this.$message.success("审核成功！");
        this.getList();
      }
    },
    async submitNopass() {
      if (this.form.reason == "") {
        return this.$message.error("原因不能为空！");
      }
      const res = await adminSystemGiveSystemVerify({
        id: this.targetId,
        verify_type: "1",
        state: "2",
        reject_reason: this.form.reason,
        charge_approve_name: localStorage.getItem("SET_NAME"),
        charge_account: localStorage.getItem("SET_ACCOUNT"),
      });
      if (res.code == 0) {
        this.$message.success("驳回成功！");
        this.targetId = [];
        this.getList();
        this.dialogVisible = false;
      }
    },
    plnopass() {
      if (this.targetId.length == 0) {
        return this.$message.error("请至少选择一条数据！");
      }
      this.form = {
        quick: "1",
        reason: "",
      };
      this.dialogVisible = true;
    },
    plpass() {
      if (this.targetId.length == 0) {
        return this.$message.error("请至少选择一条数据！");
      }
      this.pass(this.targetId);
    },
  },
  created() {
    this.getList();
  },
};
</script>

<style lang="less" scoped>
.searchBox {
  display: flex;
  align-items: center;
  .el-input {
    width: 300px;
    margin-right: 10px;
  }
  .btn {
    margin-left: 10px;
  }
}
.margin {
  margin-top: 20px;
}
</style>